from selenium import webdriver
from selenium.webdriver.common.by import By
# 1.打开浏览器
driver = webdriver.Chrome(executable_path='../01/chromedriver.exe')
# driver = webdriver.Chrome() #创建浏览器对象

"""
需求：使用cookie实现跳过登录
1). 手动登录淘宝，获取cookie
2). 使用获取到的cookie，达到登录目的，然后就可以执行登录之后的操作
"""

# 2.访问淘宝首页（URL）
driver.get("https://www.taobao.com")

# 3. 手动获取的登录态完整Cookie字符串
cookie_str = "mtop_partitioned_detect=1; _m_h5_tk=02035a67da9a4f2173ba931451d2372d_1762075794165; _m_h5_tk_enc=3eb8291ca909889c1fd1a63aa09ada31; cna=au+NIaHAxBMBASQOBH0fJU79; cookie2=10eb1648786939e6d8e87f7a5cd5bbdd; t=6df38f9c178c3f0044068d782cadea5f; _tb_token_=74ed8358fea3b; thw=xx; arms_uid=18abd420-e94a-4aca-83e9-4207f02474cf; sca=229cb55f; xlly_s=1; _samesite_flag_=true; 3PcFlag=1762066821850; wk_cookie2=17e1b4b3167c170279359038ada43daf; wk_unb=VyyRpmarTy3WQQ%3D%3D; sgcookie=E100zwV4fy1UqwA76SQO4cnPPkU8Owi87MJnNqIyEoQ%2BmxhQ%2FUvbr8zL%2FjVaBIlNwTxmhxcaEECRdNT8RnCFGktMqxsyClT81wifZUWTAEPQnHw%3D; _hvn_lgc_=0; havana_lgc2_0=eyJoaWQiOjQwMDUwMzEzNzEsInNnIjoiY2MxMDI2OTM2OTU4ODJjZmQxM2UyNGI1NjYwYzgxZGIiLCJzaXRlIjowLCJ0b2tlbiI6IjFQVDVSUk44NzZGOU5UNkw4VWVTWjhnIn0; unb=4005031371; csg=b5ebdba2; lgc=tb173303428; cancelledSubSites=empty; cookie17=VyyRpmarTy3WQQ%3D%3D; dnk=tb173303428; skt=11cc059f4b7bcc01; tracknick=tb173303428; _l_g_=Ug%3D%3D; sg=81f; _nk_=tb173303428; cookie1=AiPKuJk%2BdXUBVbGwiY8BR%2Fr0RKSxsHx6%2Bh6a8cRkLVo%3D; uc1=existShop=false&cookie15=VFC%2FuZ9ayeYq2g%3D%3D&cookie14=UoYY4dkdtnRYpA%3D%3D&cookie16=WqG3DMC9UpAPBHGz5QBErFxlCA%3D%3D&pas=0&cookie21=UtASsssme%2BBq; sn=; uc3=vt3=F8dD2kvx3D5veHbYRYg%3D&nk2=F5REOWYpKtPh%2BKc%3D&lg2=Vq8l%2BKCLz3%2F65A%3D%3D&id2=VyyRpmarTy3WQQ%3D%3D; existShop=MTc2MjA2Njg0OA%3D%3D; uc4=id4=0%40VXteU02AYom1VH7Hucx8QGtM5DoM&nk4=0%40FY4PawrqWCgAkrNkYqmokockBQ3Ixw%3D%3D; _cc_=UIHiLt3xSw%3D%3D; aui=4005031371; sdkSilent=1762095655875; havana_sdkSilent=1762095655875; havana_lgc_exp=1793171287684; fastSlient=1762067287684; tfstk=g1so37qV-a858qiSrJx7rM_RYwUvV3tB66np9HdUuIRjwbnRLp-hg6eWw9P5tMAVL4nRva3H86CcyeSp9M2HViQ-vQFWx959x5FTXlB5FntU65naX4iBYprpTrLPbZRmb5FTXodYL0k76TIuMeX2Id-eUe8E0-vBgLJFTQ8qgdv6YB5FTn-2dLvr80RrujRXLHRFTHyc3I9HYB5eY-XqT6cJNdvf0W_RxZS6XIsDEUANEPiExim9rCWyiDPNmLVB_TRmYDRwHUSAnTFoftK5D1vOwoolsT7NTN54m0YfztjH5MGz3H8FkNLlo7oHMgpH7H8mLDWD4ITeFhrZiK_VewS5ZvmeFgI9-CT0LDTp0GLN7_DQCt-yL6T1v5iWa9W1fNKgxm-k8gPquVz62D94pi0IRUJXnCI9U-5ITkO3k-2m7fYyhL7Qn-0IRUJXnCe0nVmkzK9PR; isg=BDAwbgjRLTTlL_Ew2xyMLRA9Af6CeRTDzSMzVyqB-gte5dCP0opeU5XHP-2F9cyb"

# 4. 拆分Cookie字符串并注入浏览器
# 按"; "分割Cookie，避免分割value中的"="（split('=',1)只分割第一个"="）
for cookie_item in cookie_str.split('; '):
    # 分割key和value（处理value中可能包含=的情况）
    if '=' in cookie_item:
        key, value = cookie_item.split('=', 1)
        # 构造单个Cookie字典
        single_cookie = {
            'name': key,
            'value': value,
            'domain': '.taobao.com',  # 淘宝Cookie通用域名
            'path': '/',
            'httpOnly': False,  # 适配大部分淘宝Cookie
            'secure': False     # 非HTTPS环境也能生效
        }
        try:
            driver.add_cookie(single_cookie)
            print(f"成功添加Cookie: {key}")
        except Exception as e:
            # 部分Cookie可能因格式限制添加失败，不影响整体使用
            print(f"跳过Cookie {key}: {str(e)[:50]}")

# 5. 刷新页面使Cookie生效
driver.refresh()

# 6. 退出浏览器
# driver.quit()




